<template>
  <view class="recei">
    <view class="rcece" v-for="(item, index) in address" :key="index">
      <view class="recei-top">
        <view class="img">
          <image src="/static//image/收货地址.png" mode="scaleToFill" />
        </view>
        <view class="text">
          <view class="name">
            <view class="name-left">{{ item.name }}</view>
            <view class="name-right">{{ item.mobile }}</view>
          </view>
          <view class="adders"> {{ item.area }}-{{ item.address }} </view>
        </view>
      </view>
      <view class="recei-bottom">
        <view class="radio">
          <uv-radio-group
            v-model="item.switch"
            @change="setDefault(item.id)"
          >
            <uv-radio
              :customStyle="{ margin: '8px' }"
              label="默认地址"
              name="默认地址"
            >
            </uv-radio>
          </uv-radio-group>
        </view>
        <view class="edit">
          <image
            src="/static/image/删除.png"
            mode="scaleToFill"
            @click="del(index)"
          />
          <image
            src="/static/image/编辑.png"
            mode="scaleToFill"
            @click="open(item.id)"
          />
        </view>
      </view>
    </view>
    <view class="btn">
      <uv-button
        type="primary"
        text="新增地址"
        shape="circle"
        color="#008c8c"
        @click="goadd"
      ></uv-button>
    </view>
  </view>
</template>

<script setup>
import { uniRequest } from "../../../utils/request/request";
import { onLoad } from "@dcloudio/uni-app";
import { ref } from "vue";
const address = ref("");
const goadd = () => {
  uni.navigateTo({
    url: "/pages/Address/addaddress/addaddress",
  });
};
onLoad(() => {
  uniRequest(
    "address/index?server=1",
    function (err, data) {
      if (err) {
        // 错误
        console.log(err);
      } else {
        address.value = data;
        console.log(address.value);
      }
    },
    "GET"
  );
});
const setDefault = (id) => {
  uniRequest(
    "address/switch?server=1&addressId=" + id,
    (err, data) => {
      if (err) {
        console.log(err);
      } else {
        console.log("默认地址设置成功", data);
        // 可能需要更新 address 数据以反应新的状态
        address.value.forEach(item => {
          item.isDefault = (item.id === id) ? 1 : 0; // 将当前选中的地址设为默认
        });
        uni.showToast({
          title: '设置成功',
          icon: 'success'
        });
      }
    },
    "POST",{
      id:id
    }
  );
};
const del = (index) => {
  console.log(index);
  uniRequest(
    "address/delete?server=1&id=" + address.value[index].id,
    function (err, data) {
      if (err) {
        // 错误
        console.log(err);
      } else {
        address.value.splice(index, 1);
        console.log(address.value);
      }
    },
    "GET"
  );
};
const open = (id) => {
  uni.navigateTo({
    url: `/pages/Address/addaddress/addaddress?id=${JSON.stringify(id)}`,
  });
};
</script>

<style lang="scss">
@import url(./recei.css);
</style>
